package com.some;

import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

public class XinYe01 {
    public static void main(String[] args) {
        XinYe01 xinYe01 = new XinYe01();
        System.out.println(xinYe01.dateArraysSum(new String[]{"20th Oct 2051", "20th Oct 2051", "29th Feb 2004", "2nd Jan 2022"}));
    }

    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     * <p>
     * 日期字符串数组一年中的几天求和
     *
     * @param dates string字符串一维数组 日期字符串数组
     * @return int整型
     */
    public int dateArraysSum(String[] dates) {
        // write code here
        Map<String, Integer> monthMap = new HashMap<>();
        monthMap.put("Jan", 1);
        monthMap.put("Feb", 2);
        monthMap.put("Mar", 3);
        monthMap.put("Apr", 4);
        monthMap.put("May", 5);
        monthMap.put("Jun", 6);
        monthMap.put("Jul", 7);
        monthMap.put("Aug", 8);
        monthMap.put("Sep", 9);
        monthMap.put("Oct", 10);
        monthMap.put("Nov", 11);
        monthMap.put("Dec", 12);
        HashSet<String> set = new HashSet<>();
        Collections.addAll(set, dates);
        int sum = 0;
        for (String date : set) {
            sum += oneDayOfYear(date, monthMap);
        }
        return sum;

    }

    public int oneDayOfYear(String dayStr, Map<String, Integer> monthMap) {
        String[] split = dayStr.split(" ");
        int day = Integer.parseInt(split[0].replace("st", "")
                .replace("nd", "")
                .replace("rd", "")
                .replace("th", ""));
        int month = monthMap.get(split[1]);
        int year = Integer.parseInt(split[2]);
        return dayNum(year, month, day);
    }

    public int dayNum(int year, int month, int day) {
        int[] days = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        int allDays = 0;
        if ((year % 4 == 0 && year % 100 == 0) || year % 400 == 0) {
            days[1] = 29;
        }
        for (int i = 0; i < month - 1; i++) {
            allDays += days[i];
        }
        return allDays + day;
    }
}
